<template>
  <div class="app-container">
      <el-form ref="depositForm" :rules="rules" :model="depositForm" label-position="left" label-width="70px" style="width: 400px; margin-left:50px;">

        <el-form-item label="编码" prop="code">
          <el-input v-model="depositForm.code" />
        </el-form-item>
        <el-form-item label="标题" prop="title">
          <el-input v-model="depositForm.title" />
        </el-form-item>
        <el-form-item label="汇率" prop="rate">
          <el-input v-model="depositForm.rate" />
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="depositForm.remark" :autosize="{ minRows: 2, maxRows: 4}" type="textarea" placeholder="备注" />
        </el-form-item>
      </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleCannel()">
        取消
      </el-button>
      <el-button type="primary" @click="saveDeposit()">
        确认
      </el-button>
      <el-button v-if="depositForm.status.value === 5" type="danger" @click="handleDelete(depositForm)">
        删除
      </el-button>
    </div>
  </div>
</template>

<script>
import {  saveDeposit, getDepositDetail, removeDepositByID } from '@/api/finance/deposit'


import waves from '@/directive/waves' // waves directive
import { parseTime } from '@/utils'

import Pagination from '@/components/Pagination' // secondary package based on el-pagination

export default {
  name: 'ComplexTable',
  components: { Pagination },
  directives: { waves },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'info',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        start: 1,
        perpage: 10,
        title: undefined,
        sort: '+id'
      },
      showReviewer: false,
      depositForm: {
        ID: undefined,
        remark: '',
        deposit_ID: 0,
        title: '',
        status: {
          value:1
        }
      },
      rules: {
        type: [{ required: true, message: 'type is required', trigger: 'change' }],
        timestamp: [{ type: 'date', required: true, message: 'timestamp is required', trigger: 'change' }],
        title: [{ required: true, message: 'title is required', trigger: 'blur' }]
      },
      downloadLoading: false
    }
  },
  created() {
    let depositID = this.$route.query && this.$route.query.depositID
    if (depositID !== undefined) {
      depositID = parseInt(depositID)
      if (!isNaN(depositID)) {
        this.listLoading = true
        getDepositDetail(depositID).then(response => {
          if (response.status === 200) {
            this.depositForm = response.data
            console.log(this.depositForm)
          } else {
            this.$message({
              message: response.msg,
              type: 'error'
            })
          }
        })
      }
    }
  },
  methods: {
    handleCannel() {
      this.$router.push({ path: 'deposit' })
    },
    saveDeposit() {
      let _this = this
      this.$refs['depositForm'].validate((valid) => {
        if (valid) {
          try {
            _this.depositForm.status = _this.depositForm.status.value;
          } catch (error) {
            _this.depositForm.status = 6;
          }
          if(_this.depositForm.hasOwnProperty('ID')){
            if(_this.depositForm.ID !== undefined){
              _this.depositForm.depositID = _this.depositForm.ID;
            }

          }
          saveDeposit(_this.depositForm).then((response) => {
          if (response.status === 200) {
            this.$message({
              message: '操作Success',
              type: 'success'
            })
            this.$router.push({ path: 'deposit' })
          } else {
            this.$message({
              message: response.msg,
              type: 'error'
            })
          }
          })
        }
      })
    },
    handleDelete(row, index) {
      removeDepositByID(row.ID).then(() => {
        this.$notify({
          title: 'Success',
          message: 'Update Successfully',
          type: 'success',
          duration: 2000
        })
        this.getList()
      })
    }
  }
}
</script>
